<script>
  import api from '@/services/api' // 用统一的 api 实例
  import { ref } from 'vue'
  import { useRouter } from 'vue-router'

  export default {
    name: 'RegisterView',
    setup() {
      const router = useRouter()
      const username = ref('')
      const password = ref('')
      const confirmPassword = ref('')
      const email = ref('')

      const handleRegister = async () => {
        if (password.value !== confirmPassword.value) {
          alert('两次输入的密码不一致')
          return
        }
        try {
          const res = await api.post('/auth/register', {
            UserName: username.value,
            Password: password.value,
            Email: email.value
          })
          if (res.code === 0) {
            alert('注册成功！')
            router.push('/login')
          } else {
            alert(res.message)
          }
        } catch (error) {
          console.error('Register failed:', error)
          alert('注册失败，请检查网络或稍后重试')
        }
      }

      return {
        username,
        password,
        confirmPassword,
        email,
        handleRegister,
      }
    },
  }
</script>

<template>
  <div class="register-wrapper">
    <div class="register-container">
      <div class="register-header">
        <h2>注册新账号</h2>
        <p>加入我们，探索更多精彩音乐</p>
      </div>
      <form @submit.prevent="handleRegister" class="register-form">
        <div class="input-group">
          <label for="username">账号</label>
          <input type="text" id="username" v-model="username" placeholder="请输入您的账号" required />
        </div>
        <div class="input-group">
          <label for="email">邮箱</label>
          <input type="email" id="email" v-model="email" placeholder="请输入您的邮箱" required />
        </div>
        <div class="input-group">
          <label for="password">密码</label>
          <input type="password" id="password" v-model="password" placeholder="设置您的密码" required />
        </div>
        <div class="input-group">
          <label for="confirmPassword">确认密码</label>
          <input type="password" id="confirmPassword" v-model="confirmPassword" placeholder="请再次输入密码" required />
        </div>
        <div class="form-actions">
          <router-link to="/login" class="link-text">已有账号？立即登录</router-link>
        </div>
        <button type="submit" class="register-button">注册</button>
      </form>
    </div>
  </div>
</template>

<style scoped>
.register-wrapper {
  position: relative;
  min-height: 100vh;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #1e3c72 0%, #2a5298 100%);
  font-family: 'Arial', sans-serif;
  overflow: hidden;
}

/* 动态背景元素 */
.register-wrapper::before {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: rgba(255, 255, 255, 0.05);
  transform: rotate(45deg);
  animation: rotateBg 20s linear infinite;
}

@keyframes rotateBg {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

.register-container {
  background: rgba(28, 28, 28, 0.9);
  padding: 40px;
  border-radius: 15px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
  text-align: center;
  width: 90%;
  max-width: 450px;
  z-index: 10; /* 确保内容在背景之上 */
  border: 1px solid rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(5px); /* 毛玻璃效果 */
}

.register-header h2 {
  color: #45f3ff;
  font-size: 2.5em;
  margin-bottom: 10px;
  letter-spacing: 2px;
}

.register-header p {
  color: #a0a0a0;
  font-size: 1.1em;
  margin-bottom: 30px;
}

.input-group {
  margin-bottom: 25px;
  text-align: left;
}

.input-group label {
  display: block;
  color: #fff;
  font-size: 0.9em;
  margin-bottom: 8px;
  font-weight: bold;
}

.input-group input {
  width: 100%;
  padding: 12px 15px;
  border: 1px solid #444;
  border-radius: 8px;
  background-color: #333;
  color: #fff;
  font-size: 1.1em;
  transition: all 0.3s ease;
}

.input-group input::placeholder {
  color: #888;
}

.input-group input:focus {
  border-color: #45f3ff;
  box-shadow: 0 0 10px rgba(69, 243, 255, 0.5);
  outline: none;
}

.form-actions {
  display: flex;
  justify-content: flex-end; /* 注册页面只显示一个链接，靠右 */
  margin-top: -10px;
  margin-bottom: 30px;
}

.link-text {
  color: #a0a0a0;
  text-decoration: none;
  font-size: 0.9em;
  transition: color 0.3s ease;
}

.link-text:hover {
  color: #45f3ff;
}

.register-button {
  width: 100%;
  padding: 15px;
  background-color: #45f3ff;
  color: #1c1c1c;
  border: none;
  border-radius: 8px;
  font-size: 1.2em;
  font-weight: bold;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 5px 15px rgba(69, 243, 255, 0.4);
}

.register-button:hover {
  background-color: #fff;
  transform: translateY(-3px);
  box-shadow: 0 8px 20px rgba(69, 243, 255, 0.6);
}

@media (max-width: 600px) {
  .register-container {
    padding: 30px;
  }

  .register-header h2 {
    font-size: 2em;
  }

  .register-header p {
    font-size: 1em;
  }

  .input-group label,
  .link-text {
    font-size: 0.8em;
  }

  .input-group input {
    padding: 10px 12px;
    font-size: 1em;
  }

  .register-button {
    padding: 12px;
    font-size: 1.1em;
  }
}
</style>
